package net.toocruel.iqismart.web.controller.core;

import net.toocruel.iqismart.dao.core.ArticleRepository;
import net.toocruel.iqismart.dao.core.AttentionRepository;
import net.toocruel.iqismart.entity.constants.SourceType;
import net.toocruel.iqismart.entity.core.Attention;
import net.toocruel.iqismart.service.UserService;
import net.toocruel.iqismart.service.core.CategoryService;
import net.toocruel.iqismart.service.core.CommentService;
import net.toocruel.iqismart.service.core.QuestionService;
import net.toocruel.iqismart.service.core.TagService;
import net.toocruel.iqismart.web.controller.base.AbstractBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("follow")
public class FollowController extends AbstractBaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private TagService tagService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private AttentionRepository attentionRepository;

    @Autowired
    private ArticleRepository articleRepository;

    @GetMapping("{sourceType}/{sourceId}")
    @ResponseBody
    public String follow(@PathVariable SourceType sourceType,
                               @PathVariable Long sourceId ,
                               HttpServletRequest request){

        Attention one = attentionRepository.findOne((root, query, cb) -> {
            Predicate user = cb.equal(root.get("user"), getCurrentUser());
            Predicate source = null;
            switch (sourceType) {
                case question:
                    source = cb.equal(root.get("question"), questionService.findById(sourceId));
                    break;

                case article:
                    source = cb.equal(root.get("article"), articleRepository.findOne(sourceId));
                    break;

            }
            return cb.and(user, source);
        });

        if(one!=null){
            attentionRepository.delete(one);
            return "unfollowed";
        }

        Attention attention = new Attention();
        attention.setSourceType(sourceType);
        switch (sourceType){
            case question:
                attention.setQuestion(questionService.findById(sourceId));
                break;

            case article:
                attention.setArticle(articleRepository.findOne(sourceId));
                break;

        }
        attention.setUser(getCurrentUser());

        attentionRepository.save(attention);

        return "followed";
    }

    @Override
    protected String getModule() {
        return "follow";
    }
}
